import Position from "./Position"

class Food {
  // 定义一个表示食物的元素
  element: HTMLElement

  // 坐标位置
  position: Position

  // 面板格数
  readonly boardSize = 30
  // 元素大小
  readonly elementSize = 10

  constructor() {
    // 获取食物的元素标签，赋值给 element
    this.element = document.getElementById("food")!
    this.position = this.randomPosition()
  }

  // 检查食物是否被吃掉
  regenerate(): void {
    this.position = this.randomPosition()
  }

  // 随机生成食物的位置
  randomPosition(): Position {
    const x = Math.floor(Math.random() * this.boardSize)
    const y = Math.floor(Math.random() * this.boardSize)
    this.element.style.left = `${x * this.elementSize}px`
    this.element.style.top = `${y * this.elementSize}px`
    return new Position(x, y)
  }
}

export default Food
